<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of bpServer
 *
 * @author QuangKhoi
 */
class bpServer {
    
    /*
     * @param soap_server
     */
    public $server;
    
    public function __construct($url) {
        $this->server = new soap_server();
        $this->server->configureWSDL($url);
        $this->complexType();
        //$this->registerProc("getStream", array(array("streamId" => "tns:string"), 
        //        array("start" => "tns:int"),
        //        array("length" => "tns:int")),
        //    array("return" => "tns:stream"));
        $this->registerProc("getProd", array("data" => "xsd:string"), array("return" => "xsd:string"));
        $this->registerProc("getTrendlineData", array("filter" => "tns:filter"), array("return" => "tns:trendline"));
        $this->registerProc("getTopSocialPlatformsData", array("filter" => "tns:filter"), array("return" => "tns:topSocialPlatform"));
        $this->registerProc("getSharingMetricsData", array("filter" => "tns:filter"), array("return" => "tns:sharingMetricsData"));
        $this->registerProc("getSentimentData", array("filter" => "tns:filter"), array("return" => "tns:sentiment"));
        $this->registerProc("getGenderDistributionData", array("filter" => "tns:filter"), array("return" => "tns:genderDistribution"));
        $this->registerProc("getTopLanguagesData", array("filter" => "tns:filter"), array("return" => "tns:topLanguages"));
        $this->registerProc("getGeographicDistributionData", array("filter" => "tns:filter"), array("return" => "tns:geographicDistribution"));
        $this->registerProc("getExposureData", array("filter" => "tns:filter"), array("return" => "tns:exposure"));
        $this->registerProc("getConversationMapData", array("filter" => "tns:filter"), array("return" => "tns:conversationMap"));
        $this->registerProc("saveStream", array("stream" => "tns:stream"), array("return" => "tns:stream"));
        $this->registerProc("addTask", array("userId" => "xsd:int", "signalId" => "xsd:int", "taskType" => "xsd:int",
            "taskDescription" => "xsd:string", "assignedTo" => "xsd:int"), array("return" => "tns:task"));
        $this->registerProc("markComplete", array("id" => "xsd:int"), array("result" => "xsd:string"));
        $this->registerProc("undoComplete", array("id" => "xsd:int"), array("result" => "xsd:string"));
        $this->registerProc("deleteTask", array("id" => "xsd:int"), array("result" => "xsd:string"));
        $this->registerProc("getLogin", array("login" => "tns:login"), array("return" => "tns:userinfo"));
        $this->registerProc("addUser", array("user" => "tns:user"), array("return" => "xsd:string"));
        $this->registerProc("getUser", array(), array("return" => "tns:userList"));
        $this->registerProc("addFolder", array("addfolder" => "tns:addfolder"), array("return" => "xsd:string"));
        $this->registerProc("getFolder", array(), array("return" => "tns:folder"));
        $this->registerProc("getFolderType", array(), array("return" => "tns:foldertype"));
        $this->registerProc("getFolderId", array("foldername" => "xsd:string"), array("return" => "xsd:int"));
        $this->registerProc("listUser", array(), array("return" => "tns:userList"));
        $this->registerProc("listStream", array("folderid" => "xsd:int"), array("return" => "tns:streamList"));
        $this->registerProc("addTag", array("messageId" => "xsd:int", "tagId" => "xsd:int", "streamId" => "xsd:int"), array("return" => "xsd:string"));
        $this->registerProc("getTags", array("foldername" => "xsd:string"), array("return" => "xsd:int"));
        $this->registerProc("getTag", array(), array("return" => "tns:tag"));
        $this->registerProc("createTag", array("tagname" => "xsd:string"), array("return" => "xsd:string"));
        $this->registerProc("deleteUser", array("userid" => "xsd:int"), array("return" => "xsd:string"));
        $this->registerProc("deleteTag", array("tagid" => "xsd:int"), array("return" => "xsd:string"));
        $this->registerProc("deleteStream", array("streamid" => "xsd:int"), array("return" => "xsd:string"));
        $this->registerProc("getInfo", array("userid" => "xsd:int"), array("return" => "xsd:userinfo"));
        $this->registerProc("getTimeZone", array("name" => "xsd:string", "location" => "xsd:string"), array("return" => "xsd:int"));
        $this->registerProc("getTimeZoneName", array("timezone" => "xsd:int"), array("return" => "xsd:string"));
        $this->registerProc("copyStream", array("streamid" => "xsd:int", "folderid" => "xsd:int"), array("return" => "xsd:string"));
        $this->registerProc("postTweet", array("message" => "xsd:string"), array("return" => "xsd:string"));
        $this->registerProc("getTwitters", array(), array("return" => "tns:accountList"));
        $this->registerProc("deleteTwitter", array("twitterid" => "xsd:int"), array("return" => "xsd:string"));
        $this->registerProc("postFacebookStatus", array("message" => "xsd:string", "link" => "xsd:string"), array("return" => "xsd:string"));
        $this->registerProc("getFacebooks", array(), array("return" => "tns:accountList"));
        $this->registerProc("deleteFacebook", array("facebookid" => "xsd:int"), array("return" => "xsd:string"));
        $this->registerProc("getAccounts", array(), array("return" => "tns:accountList"));
    }

    public function complexType() {
        $this->server->wsdl->addComplexType('stream', 'complexType', 'struct', 'stream', '', 
            array('streamId' => array('name' => 'streamId', 'type' => 'xsd:int'),
            'userId' => array('name' => 'userId', 'type' => 'xsd:int'),
            'name' => array('name' => 'name', 'type' => 'xsd:string'),
            'type' => array('name' => 'type', 'type' => 'xsd:int'),
            'query' => array('name' => 'query', 'type' => 'xsd:string'),
            'language' => array('name' => 'language', 'type' => 'xsd:int'),
            'location' => array('name' => 'location', 'type' => 'xsd:int'),
            'platform' => array('name' => 'platform', 'type' => 'xsd:int'),
            'externalSources' => array('name' => 'externalSources', 'type' => 'xsd:string'),
            'folderId' => array('name' => 'folderId', 'type' => 'xsd:int')));
        
        /*//ComplexType for StreamList
        $this->server->wsdl->addComplexType('streamList', 'complexType', 'array', 'all', 'SOAP-ENC:Array', array(),
                array('stream' => array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:stream[]')),'tns:stream');
        */
        $this->server->wsdl->addComplexType('streamList', 'complexType', 'struct', 'streamList', '',
                array('streamid' => array('name' => 'streamid', 'type' => 'xsd:int'),
                    'streamname' => array('name' => 'streamname', 'type' => 'xsd:string'),
                    'folderid' => array('name' => 'folderid', 'type' => 'xsd:int')));
        
        $this->server->wsdl->addComplexType('message', 'complexType', 'struct', 'message', '', array('messageId' => array('name' => 'signalId', 'type' => 'xsd:int'),
            'content' => array('name' => 'userId', 'type' => 'xsd:string')));

        $this->server->wsdl->addComplexType('signal', 'complexType', 'struct', 'signals', '', array('signalId' => array('name' => 'signalId', 'type' => 'xsd:int'),
            'userId' => array('name' => 'userId', 'type' => 'xsd:int'),
            'signalId' => array('name' => 'signalId', 'type' => 'xsd:int'),
            'streamID' => array('name' => 'signalId', 'type' => 'xsd:int'),
            'signalType' => array('name' => 'signalId', 'type' => 'xsd:int'),
            'date' => array('name' => 'signalId', 'type' => 'xsd:string'),
            'gender' => array('name' => 'signalId', 'type' => 'xsd:int'),
            'tag' => array('name' => 'signalId', 'type' => 'xsd:string'),
            'sentiment' => array('name' => 'signalId', 'type' => 'xsd:int'),
            'sharecount' => array('name' => 'signalId', 'type' => 'xsd:int'),
            'message' => array('name' => 'signalId', 'type' => 'xsd:message'),
            'image' => array('name' => 'signalId', 'type' => 'xsd:string'),
            'imageDescription' => array('name' => 'imageDescription', 'type' => 'xsd:string')));

        //ComplexType for Task
        $this->server->wsdl->addComplexType('task', 'complexType', 'struct', 'task', '', array('taskId' => array('name' => 'taskId', 'type' => 'xsd:int'),
            'taskName' => array('name' => 'taskName', 'type' => 'xsd:int'),
            'taskContent' => array('name' => 'taskContent', 'type' => 'xsd:int'),
            'taskUserId' => array('name' => 'taskUserId', 'type' => 'xsd:int'),
            'taskAssignedTo' => array('name' => 'taskAssignedTo', 'type' => 'xsd:int'),
            'taskDateCreated' => array('name' => 'taskDateCreated', 'type' => 'xsd:int'),
            'taskType' => array('name' => 'taskType', 'type' => 'xsd:int'),
            'signalId' => array('name' => 'signalId', 'type' => 'xsd:int'),
            'status' => array('name' => 'status', 'type' => 'xsd:int'),
            'streamId' => array('name' => 'streamId', 'type' => 'xsd:int'),
            'messageId' => array('name' => 'messageId', 'type' => 'xsd:int')));

        //ComplexType for Filter
        $this->server->wsdl->addComplexType('filter', 'complexType', 'struct', 'filter', '', array(
            'streamId' => array('name' => 'streamId', 'type' => 'xsd:int'),
            'dateFrom' => array('name' => 'dateFrom', 'type' => 'xsd:string'),
            'dateTo' => array('name' => 'dateTo', 'type' => 'xsd:string'),
            'language' => array('name' => 'language', 'type' => 'xsd:int'),
            'location' => array('name' => 'location', 'type' => 'xsd:int'),
            'platform' => array('name' => 'platform', 'type' => 'xsd:int'),
            'tag' => array('name' => 'tag', 'type' => 'xsd:int'),
            'sentiment' => array('name' => 'sentiment', 'type' => 'xsd:int'),
            'gender' => array('name' => 'gender', 'type' => 'xsd:int'),
            'reach' => array('name' => 'reach', 'type' => 'xsd:int')));

        //ComplexType for Trendline
        $this->server->wsdl->addComplexType('trendline', 'complexType', 'struct', 'trendline', '', array('total' => array('name' => 'total', 'type' => 'xsd:int'),
            'date' => array('name' => 'date', 'type' => 'xsd:string'),
            'data' => array('name' => 'data', 'type' => 'xsd:string')));
                
        //ComplexType for TopSocialPlatforms
        $this->server->wsdl->addComplexType('topSocialPlatform', 'complexType', 'struct', 'topSocialPlatform', '', 
                array('name' => array('name' => 'name', 'type' => 'xsd:string'),
            'data' => array('name' => 'data', 'type' => 'xsd:string'),
            'trend' => array('name' => 'trend', 'type' => 'xsd:string')));
        
        //ComplexType for SharingMetrics
        $this->server->wsdl->addComplexType('sharingMetricsData', 'complexType', 'struct', 'topSocialPlatform', '', 
                array('name' => array('name' => 'name', 'type' => 'xsd:string'),
            'data' => array('name' => 'data', 'type' => 'xsd:string')));
        
        //ComplexType for Login
        $this->server->wsdl->addComplexType('login', 'complexType', 'struct', 'login', '', array(
            'email' => array('name' => 'email', 'type' => 'xsd:string'),
            'password' => array('name' => 'password', 'type' => 'xsd:string')));
        
        //ComplexType for UserInfo
        $this->server->wsdl->addComplexType('userinfo', 'complexType', 'struct', 'userinfo', '', array(
            'email' => array('name' => 'email', 'type' => 'xsd:string'),
            'password' => array('name' => 'password', 'type' => 'xsd:string'),
            'name' => array('name' => 'name', 'type' => 'xsd:string'),
            'company' => array('name' => 'company', 'type' => 'xsd:string'),
            'address' => array('name' => 'address', 'type' => 'xsd:string'),
            'phone' => array('name' => 'phone', 'type' => 'xsd:string'),
            'position' => array('name' => 'position', 'type' => 'xsd:string'),
            'dateregistered' => array('name' => 'dateregistered', 'type' => 'xsd:date'),
            'token' => array('name' => 'token', 'type' => 'xsd:string'),
            'timezone' => array('name' => 'timezone', 'type' => 'xsd:int')));
        
        //ComplexType for User
        $this->server->wsdl->addComplexType('user', 'complexType', 'struct', 'user', '', array(
            'userid' => array('name' => 'userid', 'type' => 'xsd:int'),
            'email' => array('name' => 'email', 'type' => 'xsd:string'),
            'password' => array('name' => 'password', 'type' => 'xsd:string'),
            'name' => array('name' => 'name', 'type' => 'xsd:string'),
            'company' => array('name' => 'company', 'type' => 'xsd:string'),
            'address' => array('name' => 'address', 'type' => 'xsd:string'),
            'phone' => array('name' => 'phone', 'type' => 'xsd:string'),
            'position' => array('name' => 'position', 'type' => 'xsd:string'),
            'dateregistered' => array('name' => 'dateregistered', 'type' => 'xsd:date'),
            'token' => array('name' => 'token', 'type' => 'xsd:string'),
            'timezone' => array('name' => 'timezone', 'type' => 'xsd:int')));
        
        /*//ComplexType for UserList
        $this->server->wsdl->addComplexType('userList', 'complexType', 'array', 'all', 'SOAP-ENC:Array', array(),
                array(
                    'user' => array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:user[]', 'minOccurs'=>'1', 'maxOccurs'=>'unbounded')
                    ),
                    'tns:user'
                );*/
        $this->server->wsdl->addComplexType('userList', 'complexType', 'struct', 'userList', '', array(
            'userid' => array('name' => 'userid', 'type' => 'xsd:string'),
            'username' => array('name' => 'username', 'type' => 'xsd:string')));   
        
        $this->server->wsdl->addComplexType('accountList', 'complexType', 'struct', 'accountList', '', array(
            'screenname' => array('name' => 'screenname', 'type' => 'xsd:string'),
            'id' => array('name' => 'id', 'type' => 'xsd:string')));        
                
        $this->server->wsdl->addComplexType('folder', 'complexType', 'struct', 'folder', '', array(
            'folderid' => array('name' => 'folderid', 'type' => 'xsd:string'),
            'foldername' => array('name' => 'foldername', 'type' => 'xsd:string')));
        
        $this->server->wsdl->addComplexType('addfolder', 'complexType', 'struct', 'addfolder', '', array(
            'foldername' => array('name' => 'foldername', 'type' => 'xsd:string'),
            'foldertype' => array('name' => 'foldertype', 'type' => 'xsd:int')));
                
        $this->server->wsdl->addComplexType('foldertype', 'complexType', 'struct', 'foldertype', '', array(
            'typeid' => array('name' => 'typeid', 'type' => 'xsd:int'),
            'typename' => array('name' => 'typename', 'type' => 'xsd:string')));
        
        $this->server->wsdl->addComplexType('tag', 'complexType', 'struct', 'tag', '', array(
            'tagid' => array('name' => 'tagid', 'type' => 'xsd:int'),
            'tagname' => array('name' => 'tagname', 'type' => 'xsd:string')));

    }
    
    public function registerProc($procedure, $params, $returns, $style="rpc", 
            $use = "encoded", $description = "", $namespace = false, 
            $action = false) {
        $this->server->register($procedure,
                // parameter list:
                $params,
                // return value(s):
                $returns,
                // namespace:
                $namespace,
                // soapaction: (use default)
                $action,
                // style: rpc or document
                $style,
                // use: encoded or literal
                $use,
                // description: documentation for the method
                $description);
        //self::service($http_response_header);
    }
    
    public function getStream($streamID, $start, $length) {
        $stream = Stream::withId(base64_decode($streamId));
    }
    
    /*
     * SOAP Procedures
     */
    
    public function service($data) {
        $this->server->service($data);
    }
   
    public static function getAccounts() {
        return User::getAccounts();
    }
    
    public static function deleteFacebook($facebookid) {
        return Facebook::deleteFacebook($facebookid);
    }
    
    public static function getFacebooks() {
        return Facebook::getFacebooks();
    }   
    
    public static function postFacebookStatus($message, $link) {
        return Facebook::postFacebookStatus($message, $link);
    } 
    
    public static function deleteTwitter($twitterid) {
        return Twitter::deleteTwitter($twitterid);
    }
    
    public static function getTwitters() {
        return Twitter::getTwitters();
    }
    
    public static function postTweet($message) {
        return Twitter::postTweet($message);
    } 
    
    public static function copyStream($streamid, $folderid) {
        return Stream::copyStream($streamid, $folderid);
    }           
    
    public static function getTimeZoneName($timezone) {
        return User::getTimeZoneName($timezone);
    }            
                
    public static function getTimeZone($name, $location) {
        return User::getTimeZone($name, $location);
    }
    
    public static function getInfo() {
        return User::getInfo();
    }
    
    public static function createTag($tagname) {
        return User::createTag($tagname);
    }

    public static function getTag() {
        return User::getTag();
    }
    
    public static function deleteTag($tagid) {
        return Tag::deleteTag($tagid);
    }
    
    public static function getFolderId($name) {
        return Folder::getId($name);
    }

    public static function getFolderType() {
        return FolderType::getFolderType();
    }
    
    public static function deleteUser($userid) {
        return User::deleteUser($userid);
    }
    
    public static function deleteStream($streamid) {
        return Stream::deleteStream($streamid);
    }
    
    public static function listUser() {
        return User::listUser();
    }
    
    public static function listStream($folderid) {
        return Stream::listStream($folderid);
    }
    
    public static function getFolder() {
        return User::getFolder();
    }
    
    public static function addFolder($addfolder) {
        return User::addFolder($addfolder);
    }

    public static function getUser() {
        return User::getUser();
    }
    
    public static function addUser($user) {
        return User::addUser($user);
    }   
    
    public static function getTrendlineData($_filter) {
        return Metrics::getTrendlineData($_filter);
    }
    
    public static function getTopSocialPlatformsData($_filter) {
        return Metrics::getTopSocialPlatformsData($_filter);
    }
    
    public function getSharingMetricsData($_filter) {
        return Metrics::getSharingMetricsData($_filter);
    }
    
    public function getGenderDistributionData($_filter) {
        return Metrics::getGenderDistributionData($_filter);
    }
    
    public function getTopLanguagesData($_filter) {
        return Metrics::getTopLanguagesData($_filter);
    }
    
    public function getGeographicDistributionData($_filter) {
        return Metrics::getGeographicDistributionData($_filter);
    }
    
    public function getExposureData($_filter) {
        return Metrics::getExposureData($_filter);
    }
    
    public function getConversationMapData($_filter) {
        return Metrics::getConversationMapData($_filter);
    }
    
    public static function addTask($taskData){
        $task = Task::withData(TaskType::getName($taskData["taskType"]), 
                $taskData['taskDescription'], $taskData['userId'], 
                $taskData['assignedTo'], new DateTime(), $taskData['taskType'], 
                $taskData['signalId']);
        $saveSignal = Signal::withData($taskData['messageId'], $taskData['streamId']);
        $signalController = new SignalController($saveSignal, new SignalView());
        $signalController->save();
        $task->signalId = $saveSignal->signalID;
        $taskController = new TaskController($task, new TaskView());
        $result = $taskController->save();
        return array(
            'taskName' => $result->taskName,
            'taskContent' => $result->taskContent,
            'taskUserId' => $result->taskUserId,
            'taskAssignedTo' => $result->taskAssignedTo,
            'taskDateCreated' => $result->taskDateCreated->format("U"),
            'taskType' => $result->taskType,
            'signalId' => $result->signalId,
            'status' => $result->status
        );
    }

    public static function loadStream($streamId, $dateFrom, $dateTo, $language = 2, $location = 1, $platform = 1, $tags = "", $sentiment = 1, $gender = 1, $reach = 0, $index = 0, $length = 10) {
        $stream = Stream::withId($streamId);
        $streamController = new StreamController($stream, new StreamView());
        $streamController->render($dateFrom, $dateTo, $index, $length);
        include "/View/Signal/SingleSignal.php";
    }
    
    public static function loadMetrics($streamId, $dateFrom, $dateTo, $language = 2, $location = 1, $platform = 1, $tags = "", $sentiment = 1, $gender = 1, $reach = 0) {
        $stream = Stream::withId($streamId);
        $filter = array("streamId" => $streamId, "dateFrom" => $dateFrom, "dateTo" => $dateTo, "language" => $language, 
            "location" => $location, "platform" => $platform,  "tag" => $tags, "sentiment" => $sentiment, 
            "gender" => $gender, "reach" => $reach);
        $metricsController = new MetricsController(new Metrics(), new MetricsView());
        $metricsController->render($filter);
        include "/View/Metrics/MetricsViewHTML.php";
    }
    
    public static function loadTopSocial($streamId, $dateFrom, $dateTo, $language = 2, $location = 1, $platform = 1, $tags = "", $sentiment = 1, $gender = 1, $reach = 0) {
        //$stream = Stream::withId($streamId);
        $filter = array("streamId" => $streamId, "dateFrom" => $dateFrom, "dateTo" => $dateTo, "language" => $language, 
            "location" => $location, "platform" => $platform,  "tag" => $tags, "sentiment" => $sentiment, 
            "gender" => $gender, "reach" => $reach);
        $filter = array();
        $filter['streamId'] = $streamId;
        $filter['dateFrom'] = '20-09-2013';
            $filter['dateTo'] =  '25-09-2013';
        $topSocialPlatformsData = Metrics::getTopSocialPlatformsData($filter);
        var_dump($topSocialPlatformsData);
        include "/View/Metrics/TopSocialPlatformsHTML.php";
    }

    public static function markComplete($id) {
        $task = Task::withId($id);
        $taskController = new TaskController($task, new TaskView());
        if ($taskController->markComplete())
            return "succeed";
        else
            return "failed";
    }

    public static function undoComplete($id) {
        $task = Task::withId($id);
        $taskController = new TaskController($task, new TaskView());
        if ($taskController->undoComplete())
            return "succeed";
        else
            return "failed";
    }

    public static function deleteTask($id) {
        $task = Task::withId($id);
        $taskController = new TaskController($task, new TaskView());
        if ($taskController->delete()) 
            return "succeed";
        else
            return "failed";
    }

    public static function getLogin($login) {
        return User::getLogin($login);
    }
    
    public static function addTag($messageId, $tagId, $streamId) {
        $saveSignal = Signal::withData($messageId, $streamId);
        $signalController = new SignalController($saveSignal, new SignalView());
        $signalController->save();
        $tag = Tag::withId($tagId);
        if ($tag->add($saveSignal->signalID))
            return $tag->tagName;
        else
            return "failed";
    }
    
    public static function saveStream($streamData) {
        $stream = Stream::withData($streamData['userId'],
                $streamData['name'], $streamData['type'], 
                $streamData['query'], $streamData['language'],
                $streamData['location'], $streamData['location'], 
                $streamData['exteralSources'], $streamData['folderId']);
        $streamController = new StreamController($stream, new StreamView());
        if ($streamController->save())
            return (array) $stream;
        else
            return NULL;
    }
}
?>
